const app = getApp()
const publicFun = require('/utils/public.js')
import { like, unlike, ssDelete } from '/utils/request'

Component({
  mixins: [],
  data: {},
  options: {
    // 使用基础库内置的数据变化观测器
    observers: true,
  },
  props: {},
  observers:{},
  didMount() {
    this.setData({ userid: app.globalData.userid })
  },
  didUpdate() {
  },
  didUnmount() {},
  methods: {
    detailTap(e) {
      let data = e.currentTarget.dataset;
      let sshc = this.props.hc ? this.props.hc : app.globalData.hc;
      my.navigateTo({
        url: '/pages/detail/detail?ssid='+data.ssid+'&ssuserid='+data.ssuserid+'&sshc='+sshc
      })
    },
    /* 点赞 */
    getLikeState(e) { //是否赞过
      let data = e.target.dataset;
      if(app.globalData.userid){
        let params  = {
          userid: app.globalData.userid,
          dt: publicFun.getTimestamp(),
          wallid: data.id
        }
        if(data.state == 1){
          this.unlikeTap(params, data.index);
        }else{
          this.likeTap(params, data.index);
        }
      }else{
        publicFun.jumpLogin();
      }
    },
    async likeTap(params, index) { //点赞
      try{
        const res = await like(params)
        if(res.S == 1){
          this.props.list[index].IsPraise = 1;
          this.props.list[index].PraiseCount = Number(this.props.list[index].PraiseCount)+1;
          this.setData({
            list: this.props.list
          });
        }else{
          publicFun.showToast(res.M ? res.M : '操作失败');
        };
      }catch(e) {
        publicFun.showToast('失败，请稍后再试')
      }
    },
    async unlikeTap(params, index) { //取消点赞
      try{
        const res = await unlike(params)
        if(res.S == 1){
          this.props.list[index].IsPraise = 0;
          this.props.list[index].PraiseCount = Number(this.props.list[index].PraiseCount)-1;
          this.setData({
            list: this.props.list
          });
        }else{
          publicFun.showToast(res.M ? res.M : '操作失败');
        };
      }catch(e) {
        publicFun.showToast('失败，请稍后再试')
      }
    },
    deleteSS(e) {
      let data = e.currentTarget.dataset;
      my.confirm({
        title: '系统提示',
        content: '确定要删除当前的记录？',
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        success: (result) => {
          if(result.confirm == true){
            my.showLoading();
            this.successSSDelete(data.ssid, data.index)
          }
        },
      });
    },
    async successSSDelete(ssid, index) {
      try{
        let params = {
          id: ssid,
          userid: app.globalData.userid,
          HC: '', // 上一个接口的缓存
        }
        const res = await ssDelete(params)
        my.hideLoading();
        if(res.S == 1){
          publicFun.showToast('删除成功',);
          setTimeout(() => {
            my.navigateBack();
          }, 2000)
          // this.props.list.splice(index, 1);
          // this.setData({
          //   list: this.props.list
          // })
        }else{
          publicFun.showToast(res.M ? res.M : '操作失败');
        }
      }catch(e) {
        my.hideLoading();
        publicFun.showToast('失败，请稍后再试')
      }
    },
  },
});
